/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rcnss.data;

import java.sql.ResultSet;
import java.util.*;

/**
 *
 * @author Guesmi
 */
public class Reclamation extends ITable {

    private static final String Table = "RECLAMATION" ;
    private static final String Sequence = "RCNSS_RECLAMATION_SEQ";

    private int REC_ID ;
    private String REC_TITRE ;
    private String REC_MSG  ;
    private int REC_ETAT ;
    private int UTL_ID ;
    private int REC_AFF_A ;
    private int CAT_ID ;
    private int ATTCH_ID ;

    public static String etat[] = {"Ouvert", "Assigner", "Resolue", "Verifier", "Fermer"};

    public Attachement getATTCH_ID() {
        return Attachement.SearchByID(this.ATTCH_ID);
    }
    public void setATTCH_ID(int ATTCH_ID) {
        if(Attachement.SearchByID(ATTCH_ID)!=null){
        this.ATTCH_ID = ATTCH_ID;
        }
    }
    public void setATTCH_ID(Attachement att){
        setATTCH_ID(att.GetIDAttachement());
    }
    public int getCAT_ID() {
        return this.CAT_ID;
    }
    public void setCAT_ID(int CAT_ID) {
        if(Categorie.SearchByID(CAT_ID)!=null){
           this.CAT_ID = CAT_ID;
        }
    }
    public void setCAT_ID(Categorie CAT) {
        setCAT_ID(CAT.GetCodeGategorie());
    }
    public User getREC_AFF_A() {
        return User.SearchByID(this.REC_AFF_A);
    }
    public void setREC_AFF_A(int REC_AFF_A) {
        this.REC_AFF_A = REC_AFF_A;
    }
    public int getREC_ID() {
        return REC_ID;
    }
    public void setREC_ID(int REC_ID) {
        this.REC_ID = REC_ID;
    }
    public String getEtatReclamation(){return etat[this.REC_ETAT];}
    public void setEtatReclamation(int etat){this.REC_ETAT = etat;}
    public String getREC_MSG() {
        return REC_MSG;
    }
    public void setREC_MSG(String REC_MSG) {
        this.REC_MSG = REC_MSG;
    }
    public String getREC_TITRE() {
        return REC_TITRE;
    }
    public void setREC_TITRE(String REC_TITRE) {
        this.REC_TITRE = REC_TITRE;
    }
    public User getUTL_ID() {
        return User.SearchByID(this.UTL_ID);
    }
    public void setUTL_ID(int UTL_ID) {
        if(User.SearchByID(UTL_ID)!=null){
        this.UTL_ID = UTL_ID;
        }
    }
    public void setUTL_ID(User u){
        this.UTL_ID = u.getID();
    }


    public static int GetCurrentSeqNumber() throws Exception
    {
        ConnexionBD db = new ConnexionBD();
        ResultSet res = db.executerSelect("SELECT "+Sequence+".currval FROM dual");
       // if(res.next())
           // return res.getInt("CURRVAL");
        return -1;
    }

    public static int GetNextSeqNumber() throws Exception
    {
        ConnexionBD db = new ConnexionBD();
        ResultSet res = db.executerSelect("SELECT "+Sequence+".nextval FROM dual");
        if(res.next())
            return res.getInt("NEXTVAL");
        return -1;
    }

    

    public void Reclamation(){};

    public void LoadFromDB (String MAT){
        //ConnexionBD db = new ConnexionBD();
        try{
            ResultSet res = db.executerSelect(" Select * from "+ Table +" where ID_REC ='" + MAT + "'");
            if (res.next())
            {
            this.ATTCH_ID = res.getInt("ATTCH_ID");
            this.REC_ID = res.getInt("REC_ID");
            this.REC_MSG = res.getString("REC_MSG");
            this.REC_TITRE = res.getString("REC_TITRE");
            this.REC_ETAT = res.getInt("REC_ETAT");
            this.UTL_ID = res.getInt("UTL_ID");
            this.REC_AFF_A = res.getInt("REC_AFF_A");
            this.CAT_ID = res.getInt("CAT_ID");
            }
            else
                System.out.println("offff .. erreur!!");
        }catch(Exception sqle){}
    }

    public void SaveToDB() throws Exception{
                db.InsertNewLine(Table ,
                    //"REC_ID",Integer.toString(this.REC_ID),
                    "REC_TITRE","'"+this.REC_TITRE+"'",
                    "REC_MSG","'"+ this.REC_MSG +"'",
                    "REC_ETAT",Integer.toString(this.REC_ETAT),
                    "REC_AFF_A",Integer.toString(this.REC_AFF_A),
                    "UTL_ID",Integer.toString(this.UTL_ID),
                    "ATTCH_ID",Integer.toString(this.ATTCH_ID),
                    "CAT_ID",Integer.toString(this.CAT_ID));
    }


//    public Reclamation SearchByID(int COD){
//        Reclamation rec = new Reclamation();
//        ConnexionBD db = new ConnexionBD();
//        try{
//           ResultSet res = db.executerSelect("select * from "+ Table +" where REC_ID ='"+ COD +"'");
//           if(res.next())
//           {
//            rec.REC_ID = res.getInt("REC_ID");
//            rec.REC_TITRE = res.getString("REC_TITRE");
//            rec.REC_MSG = res.getString("REC_MSG");
//            rec.REC_ETAT = res.getInt("REC_ETAT");
//            rec.CAT_ID = res.getInt("CAT_ID");
//            rec.UTL_ID = res.getInt("UTL_ID");
//            rec.REC_AFF_A = res.getString("REC_AFF_A");
//            rec.ATTCH_ID = res.getInt("ATTCH_ID");
//           }
//
//           }catch(Exception sqle){}
//            return rec;
//     }


    /**************************************************************************
     *                          STATIC FUNCTIONS
     *************************************************************************/

    
    public static ArrayList<Reclamation> SearchByNom(String nom){
        ArrayList<Reclamation> list = new ArrayList<Reclamation>();
        ConnexionBD db = new ConnexionBD();
        try{
            ResultSet res = db.executerSelect("select * from "+ Table +" where REC_TITRE ='"+ nom +"'");
            while (res.next())
            {
                Reclamation rec = new Reclamation();
                rec.REC_ID = res.getInt("REC_ID");
                rec.REC_TITRE = res.getString("REC_TITRE");
                rec.REC_MSG = res.getString("REC_MSG");
                rec.REC_ETAT = res.getInt("REC_ETAT");
                rec.CAT_ID = res.getInt("CAT_ID");
                rec.UTL_ID = res.getInt("UTL_ID");
                rec.REC_AFF_A = res.getInt("REC_AFF_A");
                rec.ATTCH_ID = res.getInt("ATTCH_ID");
                list.add(rec);
            }
            }catch(Exception sqle){}
        return list ;
    }

    public static Reclamation SearchByID(int ID){
        Reclamation rec = new Reclamation();
        ConnexionBD db = new ConnexionBD();
        try{
            ResultSet res = db.executerSelect("select * from "+ Table +" where REC_ID ='"+ ID +"'");
            while (res.next())
            {
                rec.REC_ID = res.getInt("REC_ID");
                rec.REC_TITRE = res.getString("REC_TITRE");
                rec.REC_MSG = res.getString("REC_MSG");
                rec.REC_ETAT = res.getInt("REC_ETAT");
                rec.CAT_ID = res.getInt("CAT_ID");
                rec.UTL_ID = res.getInt("UTL_ID");
                rec.REC_AFF_A = res.getInt("REC_AFF_A");
                rec.ATTCH_ID = res.getInt("ATTCH_ID");
            }
            }catch(Exception sqle){}
        return rec ;
    }

    public static ArrayList<Reclamation> SearchByNomUtilisateur(int id){
        ArrayList<Reclamation> list = new ArrayList<Reclamation>();
        ConnexionBD db = new ConnexionBD();
        try{
            ResultSet res = db.executerSelect("select * from "+ Table +" where UTL_ID ='"+ id +"'");
            while (res.next())
            {
                Reclamation rec = new Reclamation();
                rec.REC_ID = res.getInt("REC_ID");
                rec.REC_TITRE = res.getString("REC_TITRE");
                rec.REC_MSG = res.getString("REC_MSG");
                rec.REC_ETAT = res.getInt("REC_ETAT");
                rec.CAT_ID = res.getInt("CAT_ID");
                rec.UTL_ID = res.getInt("UTL_ID");
                rec.REC_AFF_A = res.getInt("REC_AFF_A");
                rec.ATTCH_ID = res.getInt("ATTCH_ID");
                list.add(rec);
            }
            }catch(Exception sqle){}
        return list ;
    }

    public static ArrayList<Reclamation> SearchByAssignedTo(int id){
        ArrayList<Reclamation> list = new ArrayList<Reclamation>();
        ConnexionBD db = new ConnexionBD();
        try{
            ResultSet res = db.executerSelect("select * from "+ Table +" where REC_AFF_A ='"+ id +"'");
            while (res.next())
            {
                Reclamation rec = new Reclamation();
                rec.REC_ID = res.getInt("REC_ID");
                rec.REC_TITRE = res.getString("REC_TITRE");
                rec.REC_MSG = res.getString("REC_MSG");
                rec.REC_ETAT = res.getInt("REC_ETAT");
                rec.CAT_ID = res.getInt("CAT_ID");
                rec.UTL_ID = res.getInt("UTL_ID");
                rec.REC_AFF_A = res.getInt("REC_AFF_A");
                rec.ATTCH_ID = res.getInt("ATTCH_ID");
                list.add(rec);
            }
            }catch(Exception sqle){}
        return list ;
    }

    @Override
    public String toString()
    {
        return this.REC_TITRE;
    }
}
